{% extends 'layout/basic.html' %}
{% load static %}

{% block title %}短信登录{% endblock %}

{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
{% endblock %}

{% block content %}
    <div class="account">
        <div class="title">短信登录</div>
        <form id="LoginSmsForm" method="post" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="clearfix">
                            <div class="col-md-6" style="padding-left: 0">
                                {{ field }}
                                <span class="error_msg"></span>
                            </div>
                            <div class="col-md-6">
                                <input type="button" id="btnSms" class="btn btn-primary" value="发送验证码">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error_msg"></span>
                    </div>
                {% endif %}
            {% endfor %}
            <div class="row">
                <div class="col-xs-3">
                    <button id="login_submit" type="button" class="btn btn-primary">登录</button>
                </div>
                <div class="col-xs-9" style="height: 34px">
                    <a href="{% url 'login' %}" style="line-height: 34px" class="pull-right">用户名密码登录</a>
                </div>
            </div>
        </form>
    </div>
{% endblock %}
{% block js %}
    <script>
        $(function () {
            bindClickBtnSms();
            bindClickSubmit();
        });

        //点击登录按钮
        function bindClickSubmit() {
            $('#login_submit').click(function () {
                $('.error_msg').empty()
                $.ajax({
                    url: "{% url 'login_sms' %}",
                    type: 'post',
                    data: $('#LoginSmsForm').serialize(),//form表单中所有的字段，还包括csrf token
                    dataType: "JSON",
                    success: function (res) {
                        console.log(res.status)
                        if (res.status) {
                            location.href = res.data;
                        } else {
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }

        //点击按钮获取验证码
        function bindClickBtnSms() {
            $('#btnSms').click(function () {
                $('.error_msg').empty()
                let phone = $('#id_phone').val();
                $.ajax({
                    url: "{% url 'send_sms' %}",
                    type: 'get',
                    data: {phone: phone, tpl: 'login'},
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            sendSmsRemind();
                        } else {
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }

        //验证码发送后计时
        function sendSmsRemind() {
            let $btnSms = $('#btnSms');
            $btnSms.prop('disabled', true);
            let time = 60;
            let remind = setInterval(function () {
                $btnSms.val(time + '秒重后新发送');
                time = time - 1;
                if (time < 1) {
                    clearInterval(remind);
                    $btnSms.val('获取验证码').prop('disabled', false);
                }
            }, 1000)
        }
    </script>
{% endblock %}